package com.jlsoft.config;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jlsoft.entity.Role;
import com.jlsoft.entity.User;
import com.jlsoft.entity.UserRole;
import com.jlsoft.service.IRoleService;
import com.jlsoft.service.IUserRoleService;
import com.jlsoft.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Component
public class CustomUserDetailService implements UserDetailsService{
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {

        //根据用户名查找用户信息
        List<User> userList = userService.selectList(new EntityWrapper<User>()
            .eq("user_name", userName)
        );

        if (userList.size() != 1) {
            throw new UsernameNotFoundException("用户"+userName+"存在");
        }

        //userName 对应的数据库信息
       User user = userList.get(0);

        //根据用户信息的主键 查询用户的角色id
        List<UserRole> userRoles = userRoleService.selectList(new EntityWrapper<UserRole>()
            .eq("user_id", user.getId())
        );

        //收集用户角色id集合
        List<Long> roleIds = userRoles.stream().map(item -> item.getId()).collect(Collectors.toList());

        //角色id对应的角色名
        List<Role> roleList = roleService.selectList(new EntityWrapper<Role>()
            .in("id", roleIds)
        );

        //当前用户角色对应的权限集合
        List<GrantedAuthority> authorityList = roleList.stream().map(role -> {
            return new SimpleGrantedAuthority(role.getRoleName());
        }).collect(Collectors.toList());

        //整理当前用户的实体类
        CustomUser customUser = new CustomUser(user.getUserName(),
                user.getUserName(),
                authorityList);

        //当用用户的id
        customUser.setId(user.getId());

        //当前用户的角色集合
        customUser.setRoles(roleList);

        return customUser;
    }

//    public UserDetails withEncoder(String userName) {
//        if ("admin".equals(userName)) {
//            return new User(userName,
//                    passwordEncoder.encode("admin"),
//                    AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER,ROLE_ADMIN"));
//        } else if ("user".equals(userName)) {
//            return new User(userName,
//                    passwordEncoder.encode("user"),
//                    AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
//        }
//
//        return null;
//    }


//    @Autowired
//    private PasswordEncoder passwordEncoder;

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private IRoleService roleService;


}
